package com.example.administrator.newjiajiesong.main.activity.mvp.model;

import android.provider.SyncStateContract;
import android.util.Log;


import com.example.administrator.newjiajiesong.BaseApplication;
import com.example.administrator.newjiajiesong.main.activity.mvp.contract.FindPasswordContract;
import com.example.administrator.newjiajiesong.main.activity.mvp.contract.LoginContract;
import com.example.administrator.newjiajiesong.main.activity.mvp.contract.RegisterContract;
import com.example.administrator.newjiajiesong.main.my.EditPhoneNumberContract;
import com.example.administrator.newjiajiesong.main.my.entity.TreeUserEntity;
import com.example.administrator.newjiajiesong.network.ApiCallBack;
import com.example.administrator.newjiajiesong.network.http.ApiUtils;
import com.example.administrator.newjiajiesong.network.http.ResponseBean;
import com.example.administrator.newjiajiesong.utils.checkVersionsUtils.ProfileSpUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/04/19
 */

public class LoginModelImpl implements LoginContract.Model, RegisterContract.Model, FindPasswordContract.Model, EditPhoneNumberContract.Model {

    /**
     * 类型:  注册
     */
    public static final int TYPE_REGISTER = 1;
    /**
     * 类型:  忘记密码
     */
    public static final int TYPE_FORGET = 2;
    /**
     * 类型:  修改手机号
     */
    public static final int TYPE_EDIT = 3;
    /**
     * 类型:  登录
     */
    public static final int TYPE_LONG = 4;

    @Override
    public String getSaveMobli() {
        return ProfileSpUtils.getInstance().getKeyMobile();
    }

    @Override
    public String getSavePassword() {
        return ProfileSpUtils.getInstance().getKeyPassword();
    }

    @Override
    public void login(String mobile, String password, String code, final ApiCallBack<TreeUserEntity> callBack) {

        ApiUtils.getApi()
                .login(mobile, password, code, TYPE_LONG)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBean>() {
                    @Override
                    public void accept(@NonNull ResponseBean responseBean) throws Exception {
                        callBack.onResponse(responseBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        callBack.onFailure(throwable);
                    }
                });
    }

    @Override
    public void weChatLogin(final ApiCallBack<TreeUserEntity> callBack) {

    }

    @Override
    public void logIM() {

    }


    private LoginIMSuccessListener mListener;
    public interface LoginIMSuccessListener{
        void LoginIMSuccess(boolean isSuccess);
    }

    public void setLoginIMSuccessListener(LoginIMSuccessListener listener){
        mListener=listener;
    }

    @Override
    public void register(String mobile, String password, String stdNo, String code,
                         final ApiCallBack<TreeUserEntity> callBack) {
        ApiUtils.getApi()
                .register(mobile, password, password, code, TYPE_REGISTER, stdNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBean<TreeUserEntity>>() {
                    @Override
                    public void accept(@NonNull ResponseBean<TreeUserEntity> responseBean) throws Exception {
                        callBack.onResponse(responseBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        callBack.onFailure(throwable);
                    }
                });
    }

    @Override
    public void sendCode(int type, String mobile, final ApiCallBack callBack) {
        ApiUtils.getApi()
                .pin(mobile, type)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<ResponseBean>() {
                    @Override
                    public void accept(@NonNull ResponseBean responseBean) throws Exception {
                        callBack.onResponse(responseBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        callBack.onFailure(throwable);
                    }
                });
    }

    @Override
    public void sureEdit(String mobile, String password, String code, final ApiCallBack callBack) {
        ApiUtils.getApi().userPwd(ProfileSpUtils.getInstance().getUserProfie().getId()
                , mobile, password, password, code, TYPE_FORGET)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<ResponseBean>() {
                    @Override
                    public void accept(@NonNull ResponseBean responseBean) throws Exception {
                        callBack.onResponse(responseBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        callBack.onFailure(throwable);
                    }
                });
    }


    @Override
    public void setLogin(boolean isLogin) {
        ProfileSpUtils.getInstance().saveLoginSatus(isLogin);
    }

    @Override
    public void saveUserInfo(TreeUserEntity user) {
        ProfileSpUtils.getInstance().saveProfile(user);
    }

    @Override
    public void savePassword(String mobile, String password) {
        ProfileSpUtils.getInstance().savePassword(mobile, password);
    }

    @Override
    public void sureEdit(String phone, String code, final ApiCallBack callBack) {
        //  确认修改手机号
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        params.put("pinCode", code);
        params.put("type", "3");
        ApiUtils.getApi().userEidt(ProfileSpUtils.getInstance().getUserProfie().getUser_token()
                , params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<ResponseBean>() {
                    @Override
                    public void accept(@NonNull ResponseBean responseBean) throws Exception {
                        callBack.onResponse(responseBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        callBack.onFailure(throwable);
                    }
                });
    }

    @Override
    public void wxBindPhone(String token, String phone, String code, final ApiCallBack callBack) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone);
        params.put("pinCode", code);
        params.put("type", "3");
        ApiUtils.getApi().userEidt(token, params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<ResponseBean>() {
                    @Override
                    public void accept(@NonNull ResponseBean responseBean) throws Exception {
                        callBack.onResponse(responseBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        callBack.onFailure(throwable);
                    }
                });
    }

    @Override
    public void editPhone(String newPhoneNumber) {
        TreeUserEntity userProfie = ProfileSpUtils.getInstance().getUserProfie();
        userProfie.setPhone(newPhoneNumber);
        saveUserInfo(userProfie);
    }
}